<template>
  <div class="employees">
    <el-card>
      <pageTools :show-left="false">
        <template #right>
          <div>
            <!-- <el-button
              v-setButtonPower="'employees_importButton'"
              type="primary"
              @click="goUpload"
            >导入</el-button> -->
            <el-button
              v-setButtonPower="'employees_importButton'"
              type="primary"
              @click="goUpload"
            >导入</el-button>
            <el-button type="primary" @click="addClick">+新增员工</el-button>
          </div>
        </template>
      </pageTools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="list">
        <el-table-column label="序号" width="50">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              v-imgError="defaultImg"
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形势" prop="formOfEmployment" width="80" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column
          label="入职日期"
          prop="timeOfEntry"
          :formatter="formatterTimeOfEntry"
        />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="roleClick(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 2, 3, 4, 10]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 新增组件 -->
    <Add ref="add" @getData="getData" />
    <!-- 二维码对话框 -->
    <el-dialog title="二维码" :visible.sync="showQcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <Role ref="role" />
  </div>
</template>

<script>
import { sysUser, sysUserDelete } from '@/api/employees.js'
import employeesData from '@/api/constant/employees'
import Add from './components/add.vue'
import dayjs from 'dayjs'
import Qrcode from 'qrcode'
import Role from './components/role.vue'

export default {
  components: {
    Add,
    Role
  },

  data() {
    return {
      list: [],
      showQcode: false,
      page: {
        page: 1,
        size: 10,
        total: 100
      },
      defaultImg: require('@/assets/common/head.jpg')
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    async getData() {
      const res = await sysUser(this.page)
      this.page.total = res.data.total
      // 数据类型的转换 从源头进行转换
      // console.log(res.data.rows)
      res.data.rows.forEach((item) => {
        // 调用 formatter函数前两个值为空
        // 将formOfEmployment里面的数据传给函数
        item.formOfEmployment = this.formatter(
          null,
          null,
          item.formOfEmployment
        )
        // 正式
        // console.log(item.formOfEmployment)
      })
      this.list = res.data.rows
    },
    formatter(row, column, cellValue, index) {
      // 遍历枚举中的hireType数组
      // id和传过来的值相等修全等时返回给temp
      const temp = employeesData.hireType.find((item) => {
        // console.log(item.id)
        return item.id === +cellValue
      })
      // console.log(temp)
      // 当temp对象是否有值 true就返回给 formOfEmployment false返回空字符串
      return temp ? temp.value : ''
    },
    // 将时间转为所需的格式
    formatterTimeOfEntry(row, column, cellValue, index) {
      // 通过dayjs将需要格式的数据转换为YYYY-MM-DD
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    // 新添加员工按钮
    addClick() {
      // 通过ref传值将对话框打开
      this.$refs.add.dialogVisible = true
    },
    // 删除按钮
    del(row) {
      // 提示用户是否需要删除
      // 同意执行.then方法
      // 发送请求将需要删除的id传给后端
      this.$confirm('你确定删除该员工吗？', '提示').then(async() => {
        await sysUserDelete(row.id)
        // 提示删除成功
        this.$message.success('删除成功')
        // 将页码跳转为第一页
        this.handleCurrentChange(1)
      })
    },
    // 员工导入模块路由
    goUpload() {
      // 通过路由传值跳转
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
    },
    // 查询模块路由
    look(row) {
      this.$router.push('/employees/detail/' + row.id)
    },
    imgClick(imgSrc) {
      // 当没有图片时提示没有二维码
      if (!imgSrc) {
        return this.$message.error('没有头像二维码')
      }
      // 打开对话框
      this.showQcode = true
      // 将二维码异步处理
      // 不然会导致第一次打开二维码没加载出来
      this.$nextTick(() => {
        // 获取canvas标签，图片地址,画布宽高
        Qrcode.toCanvas(this.$refs.canvas, imgSrc, { width: 400, height: 400 })
      })
    },
    roleClick(row) {
      this.$refs.role.isShow = true
      this.$refs.role.getUserInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 15px;
  .page {
    margin-top: 20px;
    text-align: right;
  }
  .avatar {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }
}
</style>
